var ip = 'http://' + document.location.host;
var sfunction = $(".sfunction").val();
var tip_index = 0;
$("table").on("mouseover", "td", function() {
    if(this.offsetWidth < this.scrollWidth) { //判断文本是否超出
        var content = $(this).text(); //获取文案
        if(content) {
            tip_index = layer.tips(content, this, { time: 0 , tips: [1, '#4682B4'], area: ['200px', 'auto']});
        }
    }  
})

$("table").on("mouseleave",  "td", function() {
    layer.close(tip_index);
})

//搜索
$('.btn_search').on('click',function() {
	sfunction = $(".sfunction").val();
	var table = new Table('Tbody');
	table.init();
});

//清除筛选条件
$('.btn_search_clean').on('click',function() {
    $(".sfunction").val("");
});

$(function() {
    hasLogin(); //是否登陆
    var table = new Table('Tbody');
    table.init();

    //header  Tab
    $('#gonggaoManagement').on('click', function() {
        window.location.href = 'gonggao_manage.html';
    });
    $('#memManagement').on('click', function() {
        window.location.href = 'user_manage.html';
    });
    $('#reportManagement').on('click', function() {
        window.location.href = 'report_manage.html';
    });
    $('#messageManagement').on('click', function() {
        window.location.href = 'message_manage.html';
    });
    $('#subscribeManagement').on('click', function() {
        window.location.href = 'subscribe_function.html';
    });
    $('#subscribeFunction').on('click', function() {
        window.location.href = 'subscribe_function.html';
    });
    $('#subscribeCourt').on('click', function() {
        window.location.href = 'subscribe_court.html';
    });
    $('#subscribeOpen').on('click', function() {
        window.location.href = 'subscribe_open.html';
    });

    //退出
    $('.btn_esc').on('click', function() {
        window.sessionStorage.removeItem('MANAGER');
        window.location.href = 'login_manage.html'
    });
})

//是否登录
var hasLogin = function() {
    var username = window.sessionStorage.getItem('MANAGER');
    if (!username) window.location.href = 'login_manage.html'
}

//新增弹窗
$('.btn_add').on('click',function() {
	document.getElementById("alert_shade").style.display="block";
	document.getElementById("add_box").style.display="block";
});

//Table 
function Table(obj_classname) {
    this.$obj = $('.' + obj_classname)
}

Table.prototype = {
    constructor: Table,
    init: function() {
        this._bind();
    },
    _bind: function() {
        this._create(); //创建table modal
        //this._closeModal(); //关闭模态框
    },
    _create: function() {
        var that = this;
        $.ajax({
            type: 'post',
            url: ip + '/subscribe/pageFunction?offset=0&limit=10',
            data:{"sfunction":sfunction},
            dataType: 'json',
            // contentType: 'application/json',
            success: function(res) {
                // console.log(res)
                //if (!res.count) return false;
                var totalData = res.count;
                $('.total_num').text(totalData);
                that._page(totalData);
            },
            error: function(error) {
                // console.log(error);
            }
        })
    },
    _page: function(totalData) {
        var that = this;
        $('.page_s').pagination({
            totalData: totalData,
            showData: 10,
            pageCount: ~~(totalData / 10) + 1,
            count: 2,
            isHide: false,
            keepShowPN: true,
            coping: true, //首页和尾页
            prevContent: '〈',
            nextContent: '〉',
            callback: function(api) {
                that._render((api.getCurrent() - 1) * 10)
                // console.log(api.getCurrent() - 1);
            }
        }, function(api) { //回调函数
            that._render(api.getCurrent() - 1);
            // console.log(api.getCurrent() - 1);
        })
    },
    _render: function(index) {
        var that = this;
        $.ajax({
            type: 'post',
            url: ip + '/subscribe/pageFunction?offset=' + index + '&limit=10',
            data:{"sfunction":sfunction},
            dataType: 'json',
            success: function(res) {
                //多次判断有无数据
                // console.log(res);
                //if (!res.count) return false;
                var data = res.queryData;
                that._table(data, index);
                //that._modal(data);
            },
            error: function(error) {
                // console.log(error);
            }
        })
    },
    _table: function(data, index) {
        // console.log(data);
        var html = '';
        for (var i = 0, l = data.length; i < l; i++) {
        	var tmp = data[i];
        	var json=JSON.stringify(tmp).replace(/\"/g, "'");
        	html +=
                    '<tr>' +
                    '<td class="test">' + (i + 1 + index) + '</td>' +
                    '<td>' + tmp.functionName + '</td>' +
                    '<td>' + tmp.description + '</td>' +
                    '<td>' + tmp.updateTime + '</td>' +
                    '<td class="tab_operate"><a href="javascript:void(0);" onclick ="del('+json+')"><span class="btn_delete"><i class="fa fa-trash fa-lg"></i>删除</span></a><a href="javascript:void(0);" onclick ="edit(\''+tmp.id+'\')"><span class="btn_edit"><i class="fa fa-edit fa-lg"></i>编辑</span></a></td>'
                    '</tr>';
        }
        this.$obj.html(html);
    }
}


function del(json){
	layer.confirm('您确定要删除这条信息吗？', {btn : [ '确定', '取消' ]}, 
			function(){
				layer.closeAll('dialog');
				$.ajax({
					type: 'POST',
				    url: ip + '/subscribe/delete?id='+json.id+'&name='+json.functionName,
				    success: function(data) {
				    	if(data.code == 200){
				    		var table = new Table('Tbody');
					        table.init();
				    	}else{
				    		layer.alert(data.msg, {icon: 5,title: "提示"});
				    	}
				    	
				    }
				});
			});
	
}

function edit(id){
	$.ajax({
		type: 'POST',
	    url: ip + '/subscribe/edit?id='+id,
	    success: function(data) {
	    	if(data.code == 200){
	    		document.getElementById("alert_shade").style.display="block";
	    		document.getElementById("edit_box").style.display="block";
	    		var sendData = document.getElementById('edit_iframe').contentWindow;
	    		sendData.document.getElementById('title').value = data.data.functionName;
	    		sendData.document.getElementById('description').value = data.data.description;
	    		sendData.document.getElementById('id').value = id;
	    	}else{
	    		layer.alert(data.msg, {icon: 5,title: "提示"});
	    	}
	    }
	});
}



